R
AMU
January 26, 2025
object-oriented
programming language (like Python,JavaScript).object
?Important
Objects are like boxes in which we can put things: data, functions, and even other objects. Ben Skinner
R
import those data files into R Objects
typeof()
to check the type of datatypeof()
returns the Data type or data structureDoubles
are numbers like 2.0, 2.2, 2.999R
by default all numbers are Double
Integers
are natural numbersR
by default treated as Double
Complex
like x+yiLogical
: a variable of logical type data has values like TRUE
or FALSE
Characters
represents a string values
in R
double quotes
R
Factor
represents categorical dataas.Date
is used to create Date and Time
objectshelp(as.Date)
R
R
missing data is represented by NA
meaning Not AvailableR
is NaN
meaning Not a NumberNULL
in R
represent an object with ZERO length-Inf
and Inf
represents negative and positive infinityR
R
there are certain structures followed by imported data.
c(value1,value2,...)
vectors
alsoFunctions | Explanations |
---|---|
numeric(n) |
vector with n zeros |
rep(x,n) |
Vector with n equal elements of x |
seq(x)/seq(1:x) |
Sequence from 1 to x |
seq(f,x)/seq(f:x) |
Sequence from 1 to x |
seq(f,x,s) |
Sequence from 1 to x in steps s |
Functions | Explanations |
---|---|
length(v) |
Number of elements in vector v |
max(v) |
Largest Number in vector v |
min(v) |
Smallest number in vector v |
sum(v) |
Sum of the elements in vector v |
prod(v) |
Product of elements in vector v |
sort(v) |
Sorting of the elements of vector v |
matrix
is used to create matrix [,1] [,2]
[1,] 1 2
[2,] 3 4
[3,] 5 6
dim
, cbind
and rbind
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12
[,1] [,2] [,3]
r1 1 2 3
r2 4 5 6
Eco Hist Pol
SI 1 10 20
SII 2 9 5
SIII 3 8 15
m1
with 3
R
a list is a generic collection of objectsmylist <- list(name1=component1, name2=component2, ...)
$A
[1] 10 15 20 25 30
$student
[1] "aadil"
$idm
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 0 1 0
[3,] 0 0 1
[1] "A" "student" "idm"
data.frame
or as.data.frame
to transform into data frame GDP INV
1991 200 100
1992 250 150
1993 300 100
1994 320 120
1995 400 200
[1] 200 250 300 320 400
# Generating a new variable in the data frame
macro$lnGDP <- log(macro$GDP)
# Using `with` function
macro$lnINV <- with(macro,log(INV))
#Using `attach()` function
attach(macro)
macro$total <- GDP+INV
detach(macro)
# Results
macro
GDP INV lnGDP lnINV total
1991 200 100 5.298317 4.605170 300
1992 250 150 5.521461 5.010635 400
1993 300 100 5.703782 4.605170 400
1994 320 120 5.768321 4.787492 440
1995 400 200 5.991465 5.298317 600
GDP INV lnGDP lnINV total
1991 200 100 5.298317 4.605170 300
1992 250 150 5.521461 5.010635 400
1993 300 100 5.703782 4.605170 400
1994 320 120 5.768321 4.787492 440
1995 400 200 5.991465 5.298317 600
GDP INV lnGDP lnINV total
1994 320 120 5.768321 4.787492 440
1995 400 200 5.991465 5.298317 600
THANKS